import dataclasses
from peewee import *
from forum import database
from datetime import datetime

class BaseModel(Model):
    create_time =DateTimeField(default =datetime.now,verbose_name = '创建时间')
    
    def to_json(self) ->dict:
        r = {}
        for k in self.__data__.keys():
            #判断属性是否是create_time
            if k == 'create_time':
                r[k] = str(getattr(self,k))
            else:
                r[k] = getattr(self,k)
        return r
    class Meta:
        database = database


#用户模块
class UserModel(BaseModel):
    id = CharField(primary_key=True)
    email = CharField(max_length=32,verbose_name ='账号')
    nick_name = CharField(max_length=32,verbose_name ='昵称',null = True)
    gender = IntegerField(verbose_name='性别',null=True)
    password = CharField(max_length=32,verbose_name ='密码')
    signatrue = CharField(max_length=32,verbose_name ='签名',null = True)
    pic = CharField(max_length=32,verbose_name ='头像',null = True)
    status = IntegerField(verbose_name='账号状态',default=1)

    class Meta:
        table_name = 't_user'
       
#帖子模块
class TopicModel(BaseModel):
    id =  CharField(primary_key=True)
    title = CharField(verbose_name='标题')
    imgs =  CharField(max_length=1000,verbose_name='图片')
    content =  CharField(verbose_name='内容')
    chick_num =  IntegerField(verbose_name='点击数')
    type_ =  CharField(verbose_name='类型')
    user =  ForeignKeyField(UserModel,backref='topics')

    class Meta:
        table_name = 't_topic'
        
#评论模块
class CommentModel(BaseModel):
    id = CharField(primary_key = True)
    content = CharField(verbose_name='内容')
    topic = ForeignKeyField(TopicModel,backref='comments')
    user = ForeignKeyField(UserModel,backref='comments')

    class Meta:
        table_name = 't_comment'
        
#收藏模块
# 用户  帖子
# 1位用户  收藏多个帖子
# 1个帖子  可以被多位用户收藏

class CollectionModel(BaseModel):
    topic = ForeignKeyField(TopicModel,backref='collections')
    user = ForeignKeyField(UserModel,backref='collections')

    class Meta:
        table_name = 't_collection'
        primary_key =CompositeKey('topic','user')


# 用户 关注 用户
# 1位用户 可以关注  多位用户
# 1位用户 可以被关注  多位用户
# zs -> ls ww
# ls -> zs ww
# ww -> zs ls
# 自关联的多对多关系

#关注模块
class FollowModel(BaseModel):
    to_user = ForeignKeyField(UserModel,backref='to_user')
    from_user = ForeignKeyField(UserModel,backref='from_user')

    class Meta:
        table_name = 't_follow'
        primary_key = CompositeKey('to_user','from_user')